<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 展示文件管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="addDocument">新增展示文件</el-button>
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
            </div>
            <!--<el-table :data="data" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">-->
            <el-table :data="data" border class="table">
                <!--<el-table-column type="selection" width="55" align="center"></el-table-column>-->
                <el-table-column prop="id" label="编号" sortable width="75"></el-table-column>
                <el-table-column prop="documentname" label="文件名称" width="172"></el-table-column>
                <el-table-column prop="filetype" label="文件类型" width="100"></el-table-column>
                <el-table-column prop="showtypedes" label="展示类型" width="136"></el-table-column>
                <el-table-column label="文件" width="141" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-picture-outline" @click="showDocument(scope.$index, scope.row)">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="statusdes" label="展示状态" width="180"></el-table-column>
                <el-table-column prop="created_at" label="创建时间" width="180"></el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-s-promotion" @click="updateStatus(scope.$index, scope.row)">发布</el-button>
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="75px">
                <el-form-item label="文件名称">
                    <el-input v-model="form.documentname"></el-input>
                </el-form-item>
                <el-form-item label="显示类型">
                    <el-select v-model="form.showtypedes" placeholder="显示类型">
                        <el-option label="首页展示" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="上传文件">
                    <el-upload
                            class="upload-demo"
                            action="https://www.yiliyuan.vip/index.php/api/upload"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :on-success="addPathInfo"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="立即展示">
                    <el-switch v-model="form.status"></el-switch>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!--文件展示-->
        <el-dialog
                title="文件展示"
                :visible.sync="dialogVisible"
                width="300px"
                height="200px"
                :before-close="handleClose">
            <div class="demo-image__placeholder">
                <div class="block">
                    <el-image :src="picturepath"></el-image>
                </div>
            </div>
        </el-dialog>
        <!--文件展示状态更新-->
        <el-dialog title="提示" :visible.sync="updateVisible" width="300px" center>
            <div class="del-dialog-cnt">确认发布展示</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateDocumentStatus">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'basetable',
        data() {
            return {
                url: '/documents',
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                dialogVisible: false,
                updateVisible:false,
                form: {
                    id: '',
                    documentname: '',
                    showtype: '',
                    showtypedes: '',
                    path: '',
                    status: '',
                    statusdes:''
                },
                idx: -1,
                fileList: [],
                picturepath: ''
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    if(d.showtype == 1){
                        d.showtypedes = '首页展示';
                    }
                    if(d.status == 0){
                        d.statusdes = '未展示';
                    }else if(d.status == 1){
                        d.statusdes = '已展示';
                    }
                    return d;
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            getData() {
                //暂时不适用分页处理
                this.$axios.get(this.url)
                    .then((res) => {
                        this.tableData = res.data.data;
                    })
            },
            search() {
                this.is_search = true;
                this.$axios.get(this.url +'?documentname='+ this.select_word)
                    .then((res) => {
                        this.tableData = res.data.data;
                    })
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                this.idx = index;
                const item = this.tableData[index];
                this.form = {
                    id: item.id,
                    documentname: item.documentname,
                    showtype: item.showtype,
                    showtypedes:item.showtypedes,
                    path: item.path,
                    status: item.status,
                    statusdes: item.statusdes
                }
                this.editVisible = true;
            },
            updateStatus(index, row){
                this.idx = index;
                const item = this.tableData[index];
                this.form = {
                    id: item.id,
                    documentname: item.documentname,
                    showtype: item.showtype,
                    showtypedes:item.showtypedes,
                    path: item.path,
                    status: item.status,
                    statusdes: item.statusdes
                };
                this.updateVisible = true;

            },
            handleDelete(index, row) {
                this.idx = index;
                const item = this.tableData[index];
                this.form = {
                    id: item.id,
                    documentname: item.documentname,
                    showtype: item.showtype,
                    path: item.path,
                    status: item.status
                }
                this.delVisible = true;
            },
            showDocument(index, row){
                this.idx = index;
                const item = this.tableData[index];
                this.dialogVisible = true;
                this.picturepath = item.path;
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑，保存新增
            saveEdit() {
                if(this.form.id != ''){
                    if(this.form.showtypedes == '首页展示'){
                        this.form.status = 1;
                    }
                    this.$axios.post('/documents/'+this.form.id,this.form)
                        .then((res) => {
                            if(res.data.code == 0){
                                this.editVisible = false;
                                this.$message.success(`修改第 ${this.idx+1} 行成功`);
                                this.getData();
                            }else{
                                this.editVisible = false;
                                this.$message.error(`修改第 ${this.idx+1} 行失败，请重试`);
                            }
                        })
                }else{
                    this.$axios.post('/documents',this.form)
                        .then((res) => {
                            if(res.data.code == 0){
                                this.editVisible = false;
                                this.$message.success(`新增成功`);
                                this.getData();
                            }else{
                                this.$message.error(`新增失败，请重试`);
                            }
                        })
                }

            },
            // 确定删除
            deleteRow(){
                this.$axios.delete('/documents/'+this.form.id)
                    .then((res) => {
                        if(res.data.code == 0){
                            this.delVisible = false;
                            this.tableData.splice(this.idx,1);
                            this.$message.success(`删除成功`);
                        }else{
                            this.$message.error(`删除失败，请重试`);
                        }
                    })
            },
            handleRemove(file, fileList){
                console.log(file,fileList);
            },
            handlePreview(file){
                console.log(file);
            },
            handleClose(){
                this.dialogVisible = false;
            },
            addPathInfo(response,file,fileList){
                this.form.path = response.data;
            },
            addDocument(){
                this.editVisible = true;
                this.form = {
                    id: '',
                    documentname: '',
                    showtype: '',
                    showtypedes: '',
                    path: '',
                    status: '',
                    statusdes:''
                }
            },
            updateDocumentStatus(){
                this.$axios.post('/updatestatus/'+this.form.id)
                    .then((res) => {
                        if(res.data.code == 0){
                            this.updateVisible = false;
                            this.$message.success(`发布成功`);
                            this.getData();
                        }else{
                            this.$message.error(`发布失败，请重试`);
                        }
                    })
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .mr10{
        margin-right: 10px;
    }
</style>
